package servlets;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import util.DB_Connection;

import java.sql.*;

/**
 * Servlet implementation class Servlet_AddUser
 */
public class Servlet_AddUser extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Servlet_AddUser() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		
		
		try {
			Class.forName("com.mysql.jdbc.Driver");
			Connection connection = DB_Connection.getConnection();
			Statement st = connection.createStatement();
			//ResultSet result = st.executeQuery("SELECT * FROM users");
			String query = "INSERT INTO users (name, pass) values(?, ?)";
			////PreparedStatement stm = connection.prepareStatement("INSERT INTO users('name', 'pass') values('" + username + "', '" + password + "')");
			PreparedStatement stm = connection.prepareStatement(query);
			stm.setString(1, username);
			stm.setString(2, password);
			//st.executeUpdate("INSERT INTO users(name, pass) values(" + username + ", " + password + ")");
			stm.execute();
			connection.close();
			
			RequestDispatcher dispatcher = request.getRequestDispatcher("NewUserView.jsp");
			dispatcher.forward(request, response);
			
			
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		
	}

}
